<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admincp extends Controller {

        // Used for registering and changing password form validation
	var $min_username = 4;
	var $max_username = 20;
	var $min_password = 4;
	var $max_password = 20;

	function Admincp()
	{
		parent::Controller();
		$this->load->library('DX_Auth');
                $this->load->library('user_agent');
                $this->load->library('Form_validation');
                $this->load->model('publicdata');
                $this->load->model('syllabus/syllabus_model');

	}

	function index()
	{
	//代码
            if (!$this->dx_auth->is_admin())
                {
                      redirect('/auth/login/','location');
                 };
            $data['username'] = $this->dx_auth->get_username();

            $setting = $this->publicdata->get_setting_sitename()->result();
            $data['sitetitle'] = $setting[0]->value;
            $this->load->view('admin/admin',$data);



	}


        /**
	*管理员系统信息
	*/
        function system()
	{
	//代码
            if (!$this->dx_auth->is_admin())
                {
                      redirect('/auth/login/','location');
                 };
            $data['browser'] = $this->agent->browser();
            
            $this->load->view('admin/system',$data);

	}

        

        function adduser()
	{

		if ( $this->dx_auth->is_logged_in() AND $this->dx_auth->allow_registration)
		{
			$val = $this->form_validation;
			// Set form validation rules
			$val->set_rules('username', 'Username', 'trim|required|xss_clean|min_length['.$this->min_username.']|max_length['.$this->max_username.']|callback_username_check|alpha_dash');
			$val->set_rules('password', 'Password', 'trim|required|xss_clean|min_length['.$this->min_password.']|max_length['.$this->max_password.']|matches[confirm_password]');
			$val->set_rules('confirm_password', 'Confirm Password', 'trim|required|xss_clean');
			$val->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email|callback_email_check');
                        $val->set_rules('role','Role');
                        
			

			// Run form validation and register user if it's pass the validation
			if ($val->run() AND $this->dx_auth->register($val->set_value('username'), $val->set_value('password'), $val->set_value('email')))
			{
                            
                                // Set success message accordingly
				/*if ($this->dx_auth->email_activation)
				{
					$data['auth_message'] = 'You have successfully registered. Check your email address to activate your account.';
				}
				else
				{
					$data['auth_message'] = 'You have successfully registered. '.anchor(site_url($this->dx_auth->login_uri), 'Login');
				}*/

				// Load registration success page
				//$this->load->view($this->dx_auth->register_view,$data);
			}
                        
                        $this->load->view('admin/adduser');
			/*else
			{
				// Is registration using captcha
				if ($this->dx_auth->captcha_registration)
				{
					$this->dx_auth->captcha();
				}

				// Load registration page
				$this->load->view($this->admin->adduser);
			}*/
                }
		
		
	}

        //学期选择
        function period(){
            $data['year']=$this->syllabus_model->getyear();
            $query=$this->syllabus_model->get_current_year();
            if($query->result()){
                foreach($query->result() as $row){
                    $data['currentyear']=$this->syllabus_model->replace_year($row->year);
                }
            }else{
                $data['currentyear']='没有学期被激活';
            }
            //提交学期id
            if($this->input->post('year')){
                $id=$this->input->post('year');
                $path='./uploads/assign/';
                $query=$this->syllabus_model->getyear_by_id($id);
                foreach($query->result() as $row){
                    $dir_name=$row->year;
                }
                $classid=$this->syllabus_model->getClass()->result();
                if($this->syllabus_model->active_period($id)){
                    if(!file_exists("$path/$dir_name")){
                        mkdir("$path/$dir_name");
                    }
                    foreach($classid as $row){
                            if(!file_exists("$path/$dir_name/$row->name")){
                                mkdir("$path/$dir_name/$row->name");
                            }
                        }
                    $data['info']='激活成功，正在返回...';
                    $data['url']=site_url('admincp/period');
                    $this->load->view('admin/info',$data);
                    return ;
                }
            }
            $this->load->view('admin/period',$data);
        }
        
        //数据库备份
        function export(){
            $this->load->view('admin/export');
        }
}
?>